/**
 *       Copyright (c) 2012 Lukas Zaruba
 *
 *   This file is part of Robot Playground.
 *
 *   Robot Playground is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Robot Playground is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with Robot Playground. If not, see <http://www.gnu.org/licenses/>.
 */
package net.zarubsys.robotplayground.gui.joystick;

import net.zarubsys.robotplayground.core.device.IDeviceConfiguration;
import net.zarubsys.robotplayground.core.session.DeviceSessionContainer;
import net.zarubsys.robotplayground.core.session.Session;
import net.zarubsys.robotplayground.gui.joystick.plugin.JoystickGUIPlugin;
import net.zarubsys.robotplayground.gui.session.providers.ISystemObjectGUIProvider;
import net.zarubsys.robotplayground.gui.session.providers.SystemObjectGUIProviderClass;
import net.zarubsys.robotplayground.joystick.JoystickDevice;
import net.zarubsys.robotplayground.joystick.JoystickDeviceConfiguration;
import net.zarubsys.robotplayground.joystick.JoystickNativeAccessor;
import net.zarubsys.robotplayground.joystick.JoystickNativeAccessorHelper;

import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.PlatformUI;

/**
 * JoystickDeviceGUIProvider
 *
 * @author Lukas Zaruba, lukas.zaruba@gmail.com
 */
public class JoystickDeviceGUIProvider implements ISystemObjectGUIProvider {

	@Override
	public Image getImage(String id) {
		return JoystickGUIPlugin.getDefault().getImage(JoystickGUIPlugin.JOYSTICK_DEVICE);
	}

	@Override
	public String getDescription(String id) {
		return "Joystick";
	}

	@Override
	public String getDescription(Session session, int index) {
		DeviceSessionContainer cont = session.getDevices().get(index);
		String deviceId = getDeviceId(cont);
		JoystickNativeAccessor accessor = JoystickNativeAccessorHelper.getInstance().getAccessor(deviceId);
		
		if (!accessor.isAccessible()) return getDescription(cont.getDeviceId());
		String signature = accessor.readJoystickSignature();
		if (signature == null) return getDescription(cont.getDeviceId());
		JoystickNativeAccessorHelper.getInstance().closeAccessor(deviceId);
		return signature;
	}

	private String getDeviceId(DeviceSessionContainer cont) {
		IDeviceConfiguration cfg = cont.getConfiguration();
		if (!(cfg instanceof JoystickDeviceConfiguration)) {
			throw new IllegalArgumentException("Expected instance of " + JoystickDeviceConfiguration.class.getName() + " as a configuration!");
		}
		return ((JoystickDeviceConfiguration) cfg).getDevice();
	}

	@Override
	public SystemObjectGUIProviderClass getProviderClass() {
		return SystemObjectGUIProviderClass.DEVICE;
	}

	@Override
	public int addObjectToSession(Session session, String id) {
		JoystickDeviceWizard wizard = new JoystickDeviceWizard();
	    WizardDialog dialog = new WizardDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), wizard);
	    dialog.create();
	    if (dialog.open() != Window.OK) return -1;
	    session.getDevices().add(new DeviceSessionContainer(JoystickDevice.ID, new JoystickDeviceConfiguration(wizard.getDevice())));
		return session.getDevices().size() - 1;
	}

	@Override
	public boolean reconfigure(Session session, int index) {
		return false;
	}

	@Override
	public boolean isConfigurable(String id) {
		return false;
	}

	@Override
	public boolean remove(Session session, int index) {
		session.getDevices().remove(index);
		return true;
	}

	@Override
	public String validate(Session session, int index) {
		String deviceId = getDeviceId(session.getDevices().get(index));
		JoystickNativeAccessor accessor = JoystickNativeAccessorHelper.getInstance().getAccessor(deviceId);
		if (!accessor.isAccessible()) return "Cannot connect to the device";
		String signature = accessor.readJoystickSignature();
		if (signature != null) return null;
		JoystickNativeAccessorHelper.getInstance().closeAccessor(deviceId);
		return "Cannot read joystick signature"; 
	}

}

